package com.mach.platform.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.mach.platform.domain.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.NumberFormat;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Administrator
 * @Description: 安全监控1
 * @Package: com.mach.platform.controller
 * @time 2018/6/12 0012 15:48
 */
@Api(tags = { "安全监控---视频" })
@RestController
@RequestMapping(value = "$security")
public class SafetyCtrl {

	@PostMapping("dev&sums")
	public Response devices (
			@ApiParam(name = "level", value = "级别（1--总公司，2--工厂）", required = true) @RequestParam(value = "level", defaultValue = "1") Integer level,
			@ApiParam(name = "factory", value = "进入的工厂--工厂名称", required = true) @RequestParam(value = "factory", defaultValue = "all", required = false) String name) {

		Map finalMap = Maps.newHashMap();

		/** Virtual datas start **/

		Random random = new Random();
		NumberFormat numberFormat = NumberFormat.getPercentInstance();
		numberFormat.setMinimumFractionDigits(2);

		int totalNum;
		List<String> numOrding = Lists.newArrayList();
		List<Map> ordering = Lists.newArrayList();
		if (level.equals(1)) {
			totalNum = random.nextInt(12222) + 8444;
			numOrding = Stream.of("HD1", "HD2", "DL", "XX", "ZZ").collect(Collectors.toList());

		} else {
			totalNum = random.nextInt(2222) + 3244;
			numOrding = Stream.of("焊装", "冲压", "总装", "树脂", "涂装").collect(Collectors.toList());
		}

		List<Integer> templist = Lists.newArrayList();

		for (int i = 0; i < numOrding.size(); i++) {

			boolean doGenerate = true;

			while (doGenerate) {
				Integer tempInt = random.nextInt(totalNum);

				if (!templist.contains(tempInt)) {
					templist.add(tempInt);
					doGenerate = false;
				}

			}
		}

		templist = Ordering.natural().sortedCopy(templist);

		for (int i = 0; i < numOrding.size(); i++) {

			Map tempMap = Maps.newHashMap();
			tempMap.put("name", numOrding.get(i));
			Integer mapTotal = i == 0 ? templist.get(i) : templist.get(i) - templist.get(i - 1);
			tempMap.put("total", mapTotal);
			tempMap.put("onlines", Math.floor(mapTotal * 0.8));
			tempMap.put("offlines", Math.floor(mapTotal * 0.2));
			tempMap.put("percent", numberFormat.format(( (double) mapTotal ) / ( (double) totalNum )));

			ordering.add(tempMap);

		}

		ordering = new Ordering<Map>() {
			@Override
			public int compare (Map map, Map t1) {
				return Integer.parseInt(t1.get("total").toString())- Integer.parseInt(map.get("total").toString());
			}
		}.sortedCopy(ordering);

		finalMap.put("total", totalNum);
		finalMap.put("onlines", Math.floor(totalNum * 0.8));
		finalMap.put("offlines", Math.floor(totalNum * 0.2));
		finalMap.put("datas", ordering);

		/** Virtual datas end **/

		return Response.ok(finalMap);
	}

}
